import { _decorator, Component, Enum, instantiate, Node, Prefab, Skeleton, sp } from 'cc';
import { UITYPE, WEAPON, WEAPONSHOW } from '../AshFramework/Datas/Constant';
import { Weapon } from './Weapon';
import { GameData } from './GameData';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { Gun } from './Gun';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { TLWLSJ_UIManager } from './UIManager';
import { Magazine } from './Magazine';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { GameManager } from './GameManager';
import { HintController } from './HintController';
import { PrefsManager } from '../AshFramework/Managers/PrefsManager';
const { ccclass, property } = _decorator;

enum Ani {
    None = "",
    Fire = "kaiqiang",
    Reload = "huandan",
}

@ccclass('GameWeapon')
export class GameWeapon extends Component {
    @property({ type: Enum(WEAPON) })
    WeaponType: WEAPON = WEAPON.签字笔;

    Skeleton: sp.Skeleton = null;
    State: string = "";
    CB: Function = null;

    Data: Weapon = null;
    IsStop: boolean = false;
    IsReload: boolean = false;
    IsFire: boolean = false;

    protected onLoad(): void {
        this.Skeleton = this.node.getComponent(sp.Skeleton);
        this.Data = GameData.getWeaponByName(ZTool.GetEnumKeyByValue(WEAPON, this.WeaponType));
        this.Skeleton.setCompleteListener((trackEntry: sp.spine.TrackEntry) => { this.CB && this.CB() });
    }

    playAni(ani: string, loop: boolean = false, cb: Function = null) {
        if (this.State === ani) return;
        this.State = ani;
        if (this.State == Ani.None) {
            this.Skeleton.timeScale = 0;
            return;
        }
        this.Skeleton.timeScale = 1;
        this.CB = cb;
        this.Skeleton.setAnimation(0, ani, loop);
    }

    attack() {
    }

    /**
     * 
     * @returns 能否攻击
     */
    isAttack(): boolean {
        if (this.IsReload) {
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                node.getComponent(HintController).showHint(`正在装弹！`);
            })
            return false;
        } else if (this.IsFire) {
            return false;
        }
        return true;
    }

    startAttack() {
        this.IsStop = false;
        this.attack();
    }

    endAttack() {
        this.IsStop = true;
    }

    getWeapon() {
        TLWLSJ_UIManager.Instance.showDXPanel(UITYPE.武器, () => {
            TLWLSJ_UIManager.Instance.closeDXPanel();
            PrefsManager.Instance.userData.HaveWeapon.push(this.WeaponType);
            GameData.addWeaponByType(this.WeaponType);
            TLWLSJ_UIManager.Instance.showWeapon();
            PrefsManager.Instance.saveData();
        })
    }

    updateWeaponShow(magazine: Magazine) {
        if (TLWLSJ_UIManager.Instance.WeaponShowTs.Type == WEAPONSHOW.NON) {
            return;
        }

        let num: number = 0;
        if (magazine) {
            num = magazine.Bullets.length;
        }
        if (TLWLSJ_UIManager.Instance.WeaponShowTs.Type == WEAPONSHOW.FILL) {
            TLWLSJ_UIManager.Instance.WeaponShowTs.showBulletNum(num);
        } else if (TLWLSJ_UIManager.Instance.WeaponShowTs.Type == WEAPONSHOW.INSTANT) {

            TLWLSJ_UIManager.Instance.WeaponShowTs.showBulletNum(num);
            const bullets = GameData.getBulletByWeapon(this.WeaponType);
            if (bullets.length > 0) {
                TLWLSJ_UIManager.Instance.WeaponShowTs.showBullet(bullets[0].Name);
            } else {
                TLWLSJ_UIManager.Instance.WeaponShowTs.showBullet();
            }
        }
    }

    updateWeaponShowByNum(num: number) {
        if (num <= 0) num = 0;
        TLWLSJ_UIManager.Instance.WeaponShowTs.showBulletNum(num);
    }


    protected onEnable(): void {
        EventManager.on(MyEvent.ATTACKSTART, this.startAttack, this);
        EventManager.on(MyEvent.ATTACKEND, this.endAttack, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.ATTACKSTART, this.startAttack, this);
        EventManager.off(MyEvent.ATTACKEND, this.endAttack, this);
    }
}


